public class Test1 {
    //leetcode 132 分割回文串II
    public int minCut(String s) {
        //dp[i]表示[0,i]之间的子串的分割的最少次数
        int n = s.length();
        boolean[][] isPal = new boolean[n][n];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    isPal[i][j] = i + 1 < j ? isPal[i+1][j-1] : true;
                }
            }
        }
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if (isPal[0][i]) dp[i] = 0;
            else {
                for (int j = 1; j <= i; j++) {
                    if (isPal[j][i]) {
                        dp[i] = Math.min(dp[i],dp[j-1] + 1);
                    }
                }
            }
        }
        return dp[n-1];
    }
}
